package com.example.shuiyin.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse<T> {
    
    private int code;
    private String message;
    private T data;
    
    private static final int DEFAULT_ERROR_CODE = 500;
    private static final int DEFAULT_SUCCESS_CODE = 200;
    
    // 添加静态内部构建器类
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ApiResponseBuilder<T> {
        private int code;
        private String message;
        private T data;
        
        public ApiResponse<T> build() {
            return new ApiResponse<>(code, message, data);
        }
    }
    
    // 静态builder方法
    public static <T> ApiResponseBuilder<T> builder() {
        return new ApiResponseBuilder<>();
    }
    
    public static <T> ApiResponse<T> success(T data) {
        ApiResponseBuilder<T> builder = builder();
        builder.code = DEFAULT_SUCCESS_CODE;
        builder.message = "操作成功";
        builder.data = data;
        return builder.build();
    }
    
    public static <T> ApiResponse<T> success(String message, T data) {
        ApiResponseBuilder<T> builder = builder();
        builder.code = DEFAULT_SUCCESS_CODE;
        builder.message = message;
        builder.data = data;
        return builder.build();
    }
    
    public static <T> ApiResponse<T> error(int code, String message) {
        ApiResponseBuilder<T> builder = builder();
        builder.code = code;
        builder.message = message;
        return builder.build();
    }
    
    public static <T> ApiResponse<T> error(int code, String message, T data) {
        ApiResponseBuilder<T> builder = builder();
        builder.code = code;
        builder.message = message;
        builder.data = data;
        return builder.build();
    }
    
    // 添加简化版的错误方法，使用默认错误码
    public static <T> ApiResponse<T> error(String message) {
        ApiResponseBuilder<T> builder = builder();
        builder.code = DEFAULT_ERROR_CODE;
        builder.message = message;
        return builder.build();
    }
    
    // 添加支持传入HttpStatus的版本
    public static <T> ApiResponse<T> error(String message, int statusCode) {
        ApiResponseBuilder<T> builder = builder();
        builder.code = statusCode;
        builder.message = message;
        return builder.build();
    }
    
    // 添加一个无需数据参数的成功方法
    public static ApiResponse<Void> success(String message) {
        ApiResponseBuilder<Void> builder = builder();
        builder.code = DEFAULT_SUCCESS_CODE;
        builder.message = message;
        builder.data = null;
        return builder.build();
    }
    
    // 添加适用于String类型的错误方法重载
    public static ApiResponse<String> error(int code, String message, String data) {
        ApiResponseBuilder<String> builder = builder();
        builder.code = code;
        builder.message = message;
        builder.data = data;
        return builder.build();
    }
}